package com.easy.common.util;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpUtility {
	public RegexpUtility()
	{
	}

	public static String assignVar(String srcStr, Map<String, String> context)
	{
		if (Utility.isNullorEmpty(srcStr) || context == null)
			return srcStr;
		StringBuffer rtBuff = new StringBuffer();
		Pattern pattern = Pattern.compile("\\$\\{\\w+\\}");
		Matcher matcher;
		for (matcher = pattern.matcher(srcStr); matcher.find();)
		{
			int start = matcher.start();
			int end = matcher.end();
			String varExpr = srcStr.substring(start, end);
			String varValue = (String)context.get(getVar(varExpr));
			if (varValue != null)
				matcher.appendReplacement(rtBuff, varValue);
		}

		matcher.appendTail(rtBuff);
		return rtBuff.toString();
	}

	public static List<String> splitWords(String srcStr, String regExpr)
	{
		ArrayList<String> wordList = new ArrayList<String>();
		if (srcStr == null || regExpr == null)
			return wordList;
		Pattern exprPtn = Pattern.compile(regExpr);
		int startIndex;
		int endIndex;
		for (Matcher matcher = exprPtn.matcher(srcStr); matcher.find(); wordList.add(srcStr.substring(startIndex, endIndex)))
		{
			startIndex = matcher.start();
			endIndex = matcher.end();
		}

		return wordList;
	}

	public static String assignVar(String srcStr, String valueArray[])
	{
		if (valueArray == null || valueArray.length < 1)
			return srcStr;
		Map<String, String> context = new HashMap<String, String>();
		for (int i = 0; i < valueArray.length; i++)
			context.put(String.valueOf(i), valueArray[i]);

		return assignVar(srcStr, context);
	}

	private static String getVar(String expStr)
	{
		if (Utility.isNullorEmpty(expStr) || expStr.length() < 4)
			return "";
		else
			return expStr.substring(2, expStr.length() - 1);
	}

	public static List<String> getMatchedWords(String srcStr, String reg)
	{
		List<String> rtList = new ArrayList<String>();
		Pattern pattern = Pattern.compile(reg);
		String varExpr;
		for (Matcher matcher = pattern.matcher(srcStr); matcher.find(); rtList.add(varExpr))
		{
			int start = matcher.start();
			int end = matcher.end();
			varExpr = srcStr.substring(start, end);
		}

		return rtList;
	}

	public static List<String> splitJson(String srcStr)
	{
		if (srcStr == null || srcStr.length() < 1)
			return new ArrayList<String>();
		srcStr = srcStr.trim();
		if (srcStr.startsWith("{") && srcStr.endsWith("}"))
			srcStr = srcStr.substring(1, srcStr.length() - 1);
	    srcStr = srcStr.trim().replace("\r\n", "").replace("\n", "").replace("\\\"", "{{quote}}").replace("\\\'", "{{squote}}").replace("\\", "");
	    List<String> rtList = getMatchedWords(srcStr, "(?:(?:\"[^\"]+\")|(?:'[^']+')|(?:[^\":,]+))\\s*:\\s*(?:(?:-?\\d*\\.?\\d+(?:E\\d+)?)|(?:\"[^\"]*\")|(?:'[^']*'))");

		for (int i = 0; i < rtList.size(); i++)
		{
			String str = (String)rtList.get(i);
			rtList.set(i, str.replace("{{quote}}", "\\\"").replace("{{squote}}", "\\'"));
		}

		return rtList;
	}

	public static String replaceTitle(String srcStr, String title, String name)
	{
		title = title.replaceAll("[*]", "\\\\*");
		title = title.replaceAll("[+]", "\\\\+");
		String rtStr = null;
		Pattern pattern = Pattern.compile((new StringBuilder("([-*+/(,]+)")).append(title).append("([-*+/),]+)").toString());
		Matcher matcher = pattern.matcher(srcStr);
		rtStr = matcher.replaceAll((new StringBuilder("$1")).append(name).append("$2").toString());
		pattern = Pattern.compile((new StringBuilder("([-*+/,]+)")).append(title).append("$").toString());
		matcher = pattern.matcher(rtStr);
		rtStr = matcher.replaceAll((new StringBuilder("$1")).append(name).toString());
		pattern = Pattern.compile((new StringBuilder("^")).append(title).append("([-*+/,]+)").toString());
		matcher = pattern.matcher(rtStr);
		rtStr = matcher.replaceAll((new StringBuilder(String.valueOf(name))).append("$1").toString());
		return rtStr;
	}

	public static String cutHtml(String html)
	{
		String result = "";
		result = html.replaceAll("(<[^/\\s][\\w]*)[\\s]*([^>]*)(>)", "$1$3").replaceAll("<[^>]*>", "");
		return result;
	}
}
